package plus.easydo.server.user.mp.controller;

import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.server.user.mp.constant.UserConstants;
import plus.easydo.server.user.mp.entity.SysPost;
import plus.easydo.server.user.mp.service.ISysPostService;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "岗位信息")
@RestController
@RequestMapping("/post")
public class SysPostController extends MPBaseController {
    @Resource
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("hasPermission('system:post:list')")
    @GetMapping("/list")
    public R<List<SysPost>> list(SysPost post) {
        List<SysPost> list = postService.selectPostList(post);
        return ok(list);
    }

//    @PreAuthorize("hasPermission('system:post:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysPost post) throws IOException
//    {
//        List<SysPost> list = postService.selectPostList(post);
//        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
//        util.exportExcel(response, list, "岗位数据");
//    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("hasPermission('system:post:query')")
    @GetMapping(value = "/{postId}")
    public R<SysPost> getInfo(@PathVariable Long postId) {
        return ok(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("hasPermission('system:post:add')")
    @PostMapping
    public R<Object> add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return fail("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return fail("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(Oauth2Utils.getUserName());
        return opResult(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("hasPermission('system:post:edit')")
    @PutMapping
    public R<Object> edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(Oauth2Utils.getUserName());
        return opResult(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("hasPermission('system:post:remove')")
    @DeleteMapping("/{postIds}")
    public R<Object> remove(@PathVariable Long[] postIds) {
        return opResult(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<SysPost>>  optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return ok(posts);
    }
}
